package com.woniuxy.system.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniuxy.system.common.Result;
import com.woniuxy.system.entity.RoleUrlPermission;
import com.woniuxy.system.model.dto.RolePermissionDto;
import com.woniuxy.system.service.IRoleUrlPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色与菜单、接口权限的对应关系 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-10-13
 */
@RestController
@RequestMapping("/roleUrlPermission")
public class RoleUrlPermissionController {

    @Autowired
    private IRoleUrlPermissionService roleUrlPermissionService;

    @GetMapping("/findPermissionIdsByRoleId/{roleId}")
    public Result findPermissionIdsByRoleId(@PathVariable("roleId") Long roleId) {
        Wrapper<RoleUrlPermission> wrapper =
                Wrappers.lambdaQuery(RoleUrlPermission.class)
                        .eq(RoleUrlPermission::getRoleId,roleId);
        List<RoleUrlPermission> list = roleUrlPermissionService.list(wrapper);
        List<Long> result = list.stream()
                .map(RoleUrlPermission::getUrlPermissionId)
                .collect(Collectors.toList());
        return Result.ok(result);
    }

    @PostMapping("/roleAuthorization")
    public Result roleAuthorization(@RequestBody RolePermissionDto rolePermissionDto){
        roleUrlPermissionService.roleAuthorization(rolePermissionDto);
        return Result.ok();
    }

}
